package top.goldenyear.porpoise.framework.web.core.model.query;

import com.easy.query.core.expression.lambda.SQLActionExpression1;
import com.easy.query.core.expression.parser.core.base.WherePredicate;
import java.io.Serializable;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.noear.solon.validation.annotation.Max;
import org.noear.solon.validation.annotation.Min;
import org.noear.solon.validation.annotation.NotNull;

/**
 * @author airhead
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class QPage implements Serializable {

  public static final int PAGE_NUMBER = 1;
  public static final int PAGE_SIZE = 10;

  @NotNull(message = "页码不能为空")
  @Min(value = 1, message = "页码最小值为 1")
  @Builder.Default
  private Integer pageNumber = PAGE_NUMBER;

  @NotNull(message = "每页条数不能为空")
  @Min(value = 1, message = "每页条数最小值为 1")
  @Max(value = 100, message = "每页条数最大值为 100")
  @Builder.Default
  private Integer pageSize = PAGE_SIZE;

  public <T> SQLActionExpression1<WherePredicate<T>> buildQuery() {
    return t -> {};
  }

  public int getOffset() {
    if (pageNumber == null || pageSize == null) {
      return 0;
    }

    if (pageNumber <= 0) {
      pageNumber = PAGE_NUMBER;
    }

    return (pageNumber - 1) * pageSize;
  }
}
